package com.nbicc.anon.survey.controller;

import com.nbicc.common.RequestPageObj;
import com.nbicc.common.ResponseObj;
import com.nbicc.survey.po.Answer;
import com.nbicc.survey.po.QuestionnaireTopicEntire;
import com.nbicc.survey.service.IAnswerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Api(value = "调查问卷(匿名) 回答" , description = "回答管理")
@RestController
@Validated
@RequestMapping(value = "/anon/survey/answer")
public class AnonAnswerController {

    @Resource(name = "AnswerServiceImpl")
    private IAnswerService answerService;

    @ApiOperation(value = "添加")
    @PostMapping
    @ResponseBody
    public ResponseObj insert(@RequestBody @Valid Answer answer) throws Exception {
        answerService.insert(answer);
        return ResponseObj.success().setResult(answer);
    }

    @ApiOperation(value = "批量添加")
    @PostMapping("/batch")
    @ResponseBody
    public ResponseObj batchInsert(@RequestBody @Valid Answer[] answers) throws Exception {
        answerService.batchInsert(Arrays.asList(answers));
        return ResponseObj.success().setResult(answers);
    }


    //↑crud========================================================================

    @ApiOperation(value = "统计回答数量")
    @GetMapping("/countAnswerersNumber/questionnaireId/{questionnaireId}/topicId/{topicId}")
    @ResponseBody
    public ResponseObj countAnswererByQuestionnaireIdAndTopicId(@PathVariable String questionnaireId,@PathVariable String topicId){
        Integer numberOfAnswerer = answerService.countAnswererByQuestionnaireIdAndTopicId(questionnaireId,topicId);
        return ResponseObj.success().setResult(numberOfAnswerer);
    }

    @ApiOperation(value = "结果聚合",notes = "只有选择题才能调用此接口")
    @GetMapping("/resultAggregator/questionnaireId/{questionnaireId}/topicId/{topicId}")
    @ResponseBody
    public ResponseObj resultAggregator(@PathVariable String questionnaireId,@PathVariable String topicId){
        List<Map<String, Serializable>> resultAggregator = answerService.resultAggregator(questionnaireId,topicId);
        return ResponseObj.success().setResult(resultAggregator);
    }
    @ApiOperation(value = "以问卷批量查询结果",notes = "只有选择题有统计结果")
    @GetMapping("/questionnaire/questionnaireId/{questionnaireId}/statistics/resultAggregator")
    @ResponseBody
    public ResponseObj resultAggregator(@PathVariable String questionnaireId) {
        QuestionnaireTopicEntire questionnaireTopicEntire = answerService.statisticsQuestionnaire(questionnaireId);
        return ResponseObj.success().setResult(questionnaireTopicEntire);
    }

    @ApiOperation(value = "按条件(分页等)")
    @PostMapping("/find")
    @ResponseBody
    public ResponseObj find(@RequestBody RequestPageObj<Map<String,Object>> requestPageObj){
        ResponseObj responseObj = answerService.find(requestPageObj);
        return responseObj;
    }

}
